package com.sx.zongzhi.adapter;


import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;

import com.sx.zongzhi.R;
import com.sx.zongzhi.bean.BusinessBean;
import com.sx.zongzhi.bean.PopBusinessRoadBean;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XZY on 2017/12/4.
 * 业务信息选择的adapter 铁路沿线人员
 */

public class BusinessSelect2Adapter extends CommonAdapter<BusinessBean.SubTypesBean> {


    private List<PopBusinessRoadBean> selectList;
    private boolean isSelect=false;
    BusinessCallback mCallback;
    private Context context;

    public BusinessSelect2Adapter(Context context, int layoutId, List<BusinessBean.SubTypesBean> datas) {
        super(context, layoutId, datas);
        this.context=context;
    }

    @Override
    protected void convert(ViewHolder viewHolder, final BusinessBean.SubTypesBean item, int position) {
        viewHolder.setText(R.id.tvName,item.getSubTypeCname());
        Spinner spinner=viewHolder.getView(R.id.spType);
        List<BusinessBean.OrgsBean> orgsBeanList=new ArrayList<>();
        orgsBeanList.addAll(item.getOrgs());
        orgsBeanList.add(0,new BusinessBean.OrgsBean("-1","未选择"));
        SpinnerBusinessAdapter bAdapter=new SpinnerBusinessAdapter(context,R.layout.item_text,orgsBeanList);
        spinner.setAdapter(bAdapter);
        if (selectList!=null){
           for (PopBusinessRoadBean roadBean:selectList){
               if (item.getSubTypeKeyName().equals(roadBean.getType())){
                   for (int i=0;i<orgsBeanList.size();i++){
                       if (roadBean.getOrgId().equals(orgsBeanList.get(i).getId())){
                           spinner.setSelection(i);
                           break;
                       }
                   }
                   break;
               }
           }
       }
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (mCallback!=null){
                    if (isSelect){
                        BusinessBean.OrgsBean orgsBean= (BusinessBean.OrgsBean) parent.getSelectedItem();
                        mCallback.addBusiness(new PopBusinessRoadBean(orgsBean.getId(), item.getSubTypeKeyName()));

                    }
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        });

    }

    /**
     * 定义一个接口
     */
    public interface BusinessCallback {
        void addBusiness(PopBusinessRoadBean roadBean);
    }

    /*接口回调*/
    public void getBusiness(BusinessCallback callback) {
        mCallback = callback;
    }

    public void selectData(List<PopBusinessRoadBean> lists){
        selectList=lists;
    }

    public void setSelected(boolean isSelect){
        this.isSelect=isSelect;
    }

}
